package com.example.jacky.uilibrary.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.jacky.uilibrary.R;
import com.example.jacky.uilibrary.utils.Utils;

/**
 * 手机彩票的网格布局
 */
public class LotteryGridView extends GridView {

    private TextView ball;
    private PopupWindow pop;
    private OnActionUpListener onActionUpListener;

    public void setOnActionUpListener(OnActionUpListener onActionUpListener) {
        this.onActionUpListener = onActionUpListener;
    }

    public LotteryGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // ①手指按下
        // 显示放大的号码
        // ②手指滑动
        // 更新：号码内容+显示位置
        // ③手指抬起
        // 修改手指下面的球的背景

        // 初始化pop 窗体
        View view = View.inflate(context, R.layout.il_gridview_item_pop, null);
        ball = (TextView) view.findViewById(R.id.ii_pretextView);

        pop = new PopupWindow(context);
        pop.setContentView(view);
        pop.setBackgroundDrawable(null);
        // animationStyle animation style to use when the popup appears and
        // disappears.
        // Set to -1 for the default animation, 0 for no animation,
        // or a resource identifier for an
        // explicit animation.
        pop.setAnimationStyle(0);
        // 设置pop 的大小
        pop.setWidth(Utils.dip2px(55));
        pop.setHeight(Utils.dip2px(53));
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // 当手指按下的时候，获取到点击那个球
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        int position = pointToPosition(x, y);
        // The position of the item which contains the specified point, or
        // INVALID_POSITION if the point does not intersect an item.
        if (position == INVALID_POSITION) {
            hidePop();
            return false;
        }
        TextView child = (TextView) getChildAt(position);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //接管
                this.getParent().getParent()
                        .requestDisallowInterceptTouchEvent(true);
                showPop(child);
                break;
            case MotionEvent.ACTION_MOVE:
                updatePop(child);
                break;
            case MotionEvent.ACTION_UP:
                //放行scrollview
                this.getParent().getParent()
                        .requestDisallowInterceptTouchEvent(false);
                hidePop();
                if (onActionUpListener != null) {
                    onActionUpListener.onAction(child, position);
                }
                break;

            default:
                hidePop();
                break;
        }
        return super.onTouchEvent(ev);
    }

    private void hidePop() {
        if (pop.isShowing()) {
            pop.dismiss();
        }
    }

    private void updatePop(TextView child) {
        int yOffset = -(pop.getHeight() + child.getHeight());
        int xOffset = -(pop.getWidth() - child.getWidth()) / 2;
        ball.setText(child.getText());
        // width the new width, can be -1 to ignore
        // height the new height, can be -1 to ignore
        pop.update(child, xOffset, yOffset, -1, -1);
    }

    private void showPop(TextView child) {
        int yOffset = -(pop.getHeight() + child.getHeight());
        int xOffset = -(pop.getWidth() - child.getWidth()) / 2;
        ball.setText(child.getText());
        pop.showAsDropDown(child, xOffset, yOffset);
    }

    /**
     * 监听手指抬起
     *
     * @author Administrator
     */
    public interface OnActionUpListener {
        void onAction(View view, int position);
    }
}
